const fs = require('fs');
const path = require('path');

// 读取Home.vue文件
const filePath = path.join(__dirname, 'homework-master', 'chat-ai-system-backup', 'frontend', 'src', 'views', 'Home.vue');
const content = fs.readFileSync(filePath, 'utf8');

// 提取script标签内容
const scriptMatch = content.match(/<script>([\s\S]*?)<\/script>/);
if (!scriptMatch) {
  console.error('找不到script标签');
  process.exit(1);
}

const scriptContent = scriptMatch[1];

// 详细检查大括号匹配
console.log('详细检查大括号匹配...');
let braceCount = 0;
let lineNumber = 1;
let lastOpenLine = 0;
let lastCloseLine = 0;

const lines = scriptContent.split('\n');
for (let i = 0; i < lines.length; i++) {
  const line = lines[i];
  for (let j = 0; j < line.length; j++) {
    if (line[j] === '{') {
      braceCount++;
      lastOpenLine = i + 1;
      if (braceCount % 10 === 0) {
        console.log(`第${i + 1}行: 开括号, 当前计数: ${braceCount}`);
      }
    } else if (line[j] === '}') {
      braceCount--;
      lastCloseLine = i + 1;
      if (braceCount % 10 === 0 || braceCount < 0) {
        console.log(`第${i + 1}行: 闭括号, 当前计数: ${braceCount}`);
      }
    }
  }
  lineNumber++;
}

console.log(`\n大括号匹配结果: 开始${braceCount > 0 ? '缺少' : braceCount < 0 ? '多余' : '匹配'}${Math.abs(braceCount)}个${braceCount > 0 ? '闭括号' : '开括号'}`);
console.log(`最后一个开括号位置: 第${lastOpenLine}行`);
console.log(`最后一个闭括号位置: 第${lastCloseLine}行`);

// 输出最后几个函数定义来查看是否有缺失的括号
console.log('\n检查最后几个方法定义...');
const lastMethodLines = lines.slice(Math.max(0, lines.length - 50), lines.length);
console.log(lastMethodLines.join('\n'));

// 创建一个简化版本的脚本用于调试
const simplifiedScript = scriptContent
  .replace(/import.*?from.*?['"].*?['"];/g, '')
  .replace(/export default /g, 'const component = ');

const testFile = path.join(__dirname, 'test-script.js');
fs.writeFileSync(testFile, simplifiedScript);
console.log('\n简化后的脚本已保存到test-script.js');

// 尝试找到可能的缺失位置
if (braceCount > 0) {
  console.log('\n可能的问题位置：在最后一个方法或对象定义中缺少闭合括号');
} else if (braceCount < 0) {
  console.log('\n可能的问题位置：有多余的闭合括号');
}